package cn.com.darly.vpm.bizapi;

import cn.com.darly.vpm.business.BusinessApi;
import cn.com.darly.vpm.business.amuse.bean.Amuse;
import cn.com.darly.vpm.business.amuse.bean.AmuseExample;
import cn.com.darly.vpm.business.amuse.service.AmuseService;
import cn.com.darly.vpm.business.conversation.bean.ConversationMessage;
import cn.com.darly.vpm.business.conversation.bean.ConversationMessageExample;
import cn.com.darly.vpm.business.conversation.service.ConversationService;
import cn.com.darly.vpm.business.friend.bean.FriendShip;
import cn.com.darly.vpm.business.friend.service.FriendShipService;
import cn.com.darly.vpm.framework.base.BaseController;
import cn.com.darly.vpm.framework.common.DateUtil;
import cn.com.darly.vpm.framework.common.UuidGenerateUtil;
import cn.com.darly.vpm.security.user.bean.User;
import cn.com.darly.vpm.security.user.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Darly/张宇辉/2018/5/8 11:14
 * @version 1.0/cn.com.darly.vpm.bizapi
 */
@Service
public class BusinessBiz  extends BaseController implements BusinessApi {
    @Resource
    FriendShipService shipService;
    @Resource
    UserService userService;

    @Override
    public List<FriendShip> userFriend(String userid) {
        List<FriendShip> ships = shipService.findFriendByUserID(userid);
        for (FriendShip ship:ships) {
            User user = userService.findUserByID(ship.getFriendid());
            if (user !=null){
                ship.setName(user.getName());
                ship.setMobile(user.getMobile());
            }
        }
        return ships;
    }

    @Override
    public FriendShip userNewFriend(FriendShip shiper) {
        User user =  userService.findUserByID(shiper.getFriendid());
        if (user==null){
            return null;
        }

        FriendShip ship =shipService.isFriend(shiper.getUserid(),shiper.getFriendid());
        if (ship !=null){
            ship.setName(user.getName());
            ship.setMobile(user.getMobile());
            ship.setFrienddeviceId(user.getDeviceId());
            return ship;
        }else {
            shiper.setName(user.getName());
            shiper.setMobile(user.getMobile());
            shiper.setFrienddeviceId(user.getDeviceId());
            int count = shipService.save(shiper);
            if (count > 0) {
                return shiper;
            } else {
                return null;
            }
        }
    }

    @Override
    public List<FriendShip> findUserListById(String id) {
        List<FriendShip> bigData = new ArrayList<FriendShip>();
        //獲取所有想添加我的好友
        List<FriendShip> ships =  shipService.getListByLoginUserId(id);
        if (ships!=null){
            for (FriendShip friendShip:ships) {
                User user =  userService.findUserByID(friendShip.getUserid());
                if (user!=null) {
                    friendShip.setIcon(user.getIcon());
                    friendShip.setName(user.getName());
                    friendShip.setMobile(user.getMobile());
                    friendShip.setApply(false);
                    bigData.add(friendShip);
                }
            }
        }
        //主動申請的好友
        List<FriendShip> apply =  shipService.getListByFriendId(id);
        if (apply!=null){
            for (FriendShip friendShip:apply) {
                User user =  userService.findUserByID(friendShip.getUserid());
                if (user!=null) {
                    friendShip.setIcon(user.getIcon());
                    friendShip.setName(user.getName());
                    friendShip.setMobile(user.getMobile());
                    friendShip.setApply(true);
                    bigData.add(friendShip);
                }
            }
        }
        return bigData;
    }

    @Override
    public boolean passThrou(String userId, String friendId) {
        User loger = userService.findUserByID(userId);
        //判断有没有好友申请的关系(对方申请好友)
        FriendShip pass =shipService.isFriend(friendId,userId);
        if (pass!=null){
            pass.setUpdateby(loger.getName());
            pass.setUpdatetime(DateUtil.now());
            pass.setFriendup("1");
            //（我这边也申请过好友）否则，重新建立申请链接
            FriendShip apply = shipService.isFriend(userId,friendId);
            if (apply!=null){
                apply.setUpdateby(loger.getName());
                apply.setUpdatetime(DateUtil.now());
                apply.setFriendup("1");
                int i = shipService.updateById(pass);
                int a = shipService.updateById(apply);
                if (i>0&&a>0){
                    return true;
                }else {
                    return false;
                }
            }else {
                User frend = userService.findUserByID(friendId);
                apply = new FriendShip();
                apply.setId(UuidGenerateUtil.getUUIDLong());
                apply.setUserid(userId);
                apply.setFriendid(friendId);
                apply.setCreateby(loger.getName());
                apply.setCreatetime(DateUtil.now());
                apply.setUpdateby(loger.getName());
                apply.setUpdatetime(DateUtil.now());
                apply.setFriendup("1");
                apply.setFriendnickname(frend.getName());
                apply.setBackup(loger.getName()+"同意添加"+frend.getName()+"的好友申请");
                int i = shipService.updateById(pass);
                int a = shipService.save(apply);
                if (i>0&&a>0){
                    return true;
                }else {
                    return false;
                }
            }
        }else {
            return false;
        }

    }

    @Override
    public int isConnectFriendShip(String userId, String friendId) {
        FriendShip ship =shipService.isFriend(userId,friendId);
        if (ship ==null) {
            return -1;
        }else {
            String up = ship.getFriendup();
            return Integer.parseInt(up);
        }
    }

    //----------amuse------------
    @Autowired
    AmuseService amuseService;

    @Override
    public boolean publish(Amuse amuse) {
        int t = amuseService.save(amuse);
        if (t>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public List<Amuse> published(String userid) {
        AmuseExample example = new AmuseExample();
        example.createCriteria().andAuthorEqualTo(userid);
        return amuseService.selectByExample(example);
    }

    @Override
    public PageInfo<Amuse> getAmuseByPager(Integer pageNo, Integer pageSize) {
        PageInfo<Amuse> page =  amuseService.getAmuse( pageNo, pageSize);
        return page;
    }

    //----------Conversation------------
    @Autowired
    ConversationService conversationService;

    @Override
    public boolean saveConversationMsg(ConversationMessage msg) {
        int count = conversationService.save(msg);
        if (count>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public List<ConversationMessage> findChatMessage(String id, String friendid) {
        ConversationMessageExample example = new ConversationMessageExample();
        example.createCriteria().andMsgFromEqualTo(id).andMsgToEqualTo(friendid);
        List<ConversationMessage> msg = conversationService.selectByExample(example);
        return msg;
    }


}
